package com.cxb;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.app.AlertDialog.Builder;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.os.Handler.Callback;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.cxb.adapter.HistoryAdapter;
import com.cxb.adapter.HistoryAdapter.ListItemView;
import com.cxb.entity.GpsDestinationInfo;
import com.cxb.manager.HistoryManager;
import com.cxb.manager.NavigationManager;
import com.cxb.utils.Constants;
import com.cxb.utils.Constants.CXBBroadcast;
import com.cxb.view.DialogCreater;
import com.cxb.R;

public class HistoryListActivity extends BaseActivity implements Callback,
		OnItemClickListener {
	private NavigationManager navigationManager;
	private Button ibBack = null;
	private ListView lvHistoryList = null;
	private TextView tvNoHistory = null;
	private TextView tv_seleceted;
	private HistoryAdapter historyAdapter;
	private HistoryManager historyManager;
	private ProgressDialog progressDialog;
	private List<GpsDestinationInfo> historyList;
	private Handler mHandler;
	private boolean editable = false;
	private List<GpsDestinationInfo> historyListChecked;

	private View ll_edit, tv_cancel, tv_selecteall, btn_deleted;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_history);
		progressDialog = new ProgressDialog(this);
		progressDialog.setMessage(getString(R.string.reading));

		init();
		new Thread(new LoadHistoryTask()).start();
	}

	class LoadHistoryTask implements Runnable {
		@Override
		public void run() {
			List<GpsDestinationInfo> list = historyManager.getAllHistory();
			Message msg = new Message();
			msg.what = 0;
			msg.obj = list;
			mHandler.sendMessage(msg);

		}
	}

	@Override
	protected void onStart() {
		super.onStart();

		// historyList();
	}

	private void init() {
		historyManager = new HistoryManager(this);
		navigationManager = new NavigationManager(this);
		historyListChecked = new LinkedList<GpsDestinationInfo>();
		// IntentFilter mFilter = new IntentFilter();
		// mFilter.addAction(Constants.CXBBroadcast.HISTORY_REFRESH_ACTION);
		// registerReceiver(mReceiver, mFilter);
		tv_seleceted = (TextView) findViewById(R.id.tv_seleceted);
		ll_edit = findViewById(R.id.ll_edit);
		tv_cancel = findViewById(R.id.tv_cancel);
		tv_selecteall = findViewById(R.id.tv_selecteall);
		btn_deleted = findViewById(R.id.btn_deleted);
	
		
		tv_seleceted.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				titleViewEditable(true);
			}
		});
		
		tv_cancel.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				titleViewEditable(false);
			}
		});
		
		tv_selecteall.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				if(historyList != null && historyListChecked !=null)
				{
					historyListChecked.clear();
					for(int i = 0 ; i < historyList.size() ; i++)
						historyListChecked.add(historyList.get(i));
				}
				if(historyAdapter!=null)
				{
					historyAdapter.notifyDataSetChanged();
				}
			}
		});
		
		btn_deleted.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				DialogCreater dc = new DialogCreater(HistoryListActivity.this);
				final Dialog dialog = dc.getCommonDialog(null, "确定删除?");
				TextView tv_confirm  = (TextView)dc.getView(R.id.tv_confirm);
				TextView tv_cancel  = (TextView)dc.getView(R.id.tv_cancel);
				tv_confirm.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View arg0) {
						if(historyManager!=null)
						{
							for(int i = 0 ; historyListChecked != null && i < historyListChecked.size() ; i++)
							{
								historyManager.deleteHistoryById(historyListChecked.get(i).id);
								for(int j = 0 ; j < historyList.size();j++)
								{
									if(historyList.get(j).id == historyListChecked.get(i).id)
									{
										historyList.remove(j);
									}
								}
							}
						}
						if(historyAdapter!=null)
						{
							historyAdapter.notifyDataSetChanged();
							loadHistoryList();
						}
						dialog.dismiss();
					}
					
				});
				
				tv_cancel.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View arg0) {
						dialog.dismiss();
					}
				});
				
				dialog.show();
			}
		});
		
		lvHistoryList = (ListView) findViewById(R.id.lv_history);
		tvNoHistory = (TextView) findViewById(R.id.tv_no_history);
		lvHistoryList.setOnItemClickListener(this);
		
		ibBack = (Button) findViewById(R.id.ib_back);
		ibBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});

		historyList = new ArrayList<GpsDestinationInfo>();

		mHandler = new Handler(this);
		progressDialog.show();
	}
	
	private void titleViewEditable(boolean  editable)
	{
		this.editable = editable;
		if(editable)
		{
			ll_edit.setVisibility(View.VISIBLE);
			tv_seleceted.setVisibility(View.GONE);
		}else
		{
			ll_edit.setVisibility(View.GONE);
			tv_seleceted.setVisibility(View.VISIBLE);
		}
		if (historyAdapter != null)
			historyAdapter.setEditable(editable);
	}

	private void loadHistoryList() {

		if (historyList.size() != 0) {
			historyAdapter = new HistoryAdapter(this, historyList);
			historyAdapter.setHistoryListChecked(historyListChecked);
			lvHistoryList.setAdapter(historyAdapter);
			titleViewEditable(false);
			tvNoHistory.setVisibility(View.GONE);
			lvHistoryList.setVisibility(View.VISIBLE);
		} else {
			tvNoHistory.setVisibility(View.VISIBLE);
			lvHistoryList.setVisibility(View.GONE);
			tv_seleceted.setVisibility(View.GONE);
			ll_edit.setVisibility(View.GONE);
		}
		// historyAdapter.notifyDataSetChanged();
	}

	private void deleteAll() {
		historyManager.deleteAllHistory();
		historyList.clear();
		loadHistoryList();
		// historyList();
	}

	@Override
	public boolean handleMessage(Message msg) {
		if (progressDialog != null) {
			progressDialog.dismiss();
			historyList = (List<GpsDestinationInfo>) msg.obj;
		}
		loadHistoryList();
		return false;
	}

	// private BroadcastReceiver mReceiver = new BroadcastReceiver() {
	// @Override
	// public void onReceive(Context context, Intent intent) {
	// String action = intent.getAction();
	// if (action == CXBBroadcast.HISTORY_REFRESH_ACTION) {
	// historyList();
	// }
	// }
	// };
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		if (progressDialog != null)
			progressDialog.dismiss();
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View view, int arg2, long arg3) {
		if (editable) {
			if (isChecked((GpsDestinationInfo) historyAdapter.getItem(arg2))) {
				historyListChecked.remove((GpsDestinationInfo) historyAdapter
						.getItem(arg2));
			} else {
				historyListChecked.add((GpsDestinationInfo) historyAdapter
						.getItem(arg2));
			}
			historyAdapter.notifyDataSetChanged();
		}
		else
		{
			GpsDestinationInfo destination = (GpsDestinationInfo) historyAdapter.getItem(arg2);
			// // String intInfo = "nav:" + destination.destName + "," +
			// // destination.longitude + "," + destination.latitude;
			// // Intent i = new Intent(Intent.ACTION_VIEW,
			// // Uri.parse(intInfo));
			// // i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			// // context.startActivity(i);
			 navigationManager.startupNavigation(destination);
		}

	}

	private boolean isChecked(GpsDestinationInfo index) {
		for (int i = 0; i < historyListChecked.size(); i++) {
			if (historyListChecked.get(i).id == index.id) {
				return true;
			}
		}
		return false;
	}
}
